define(['jquery', 'knockout','director',
    'static/pages/widget/header/header',
    'static/pages/widget/footer/footer', 'jqcookie'], function ($, ko,Director,Header,Footer) {
    window.router = Router();
    var curRoute = "";
    var ipu_token = $.cookie('token')
    window.addRouter = function(path, func) {
        var pos = path.indexOf('/:');
        var truePath = path;
        if (pos != -1)
            truePath = path.substring(0,pos);
        func = func || function() {
            //console.log("init router:"+truePath+JSON.stringify(params));
            var params = arguments;
            curRoute = truePath
            initPage('static/pages/' + truePath,params);
        }
        var tmparray = truePath.split("/");
        if(tmparray[1] in router.routes && tmparray[2] in router.routes[tmparray[1]] && tmparray[3] in router.routes[tmparray[1]][tmparray[2]]){
            return;
        }else{
            router.on(path, func);
        }
    }


    function initPage(p, params) {
        var module = p;
        //动态修改header
        window.renderHeader && window.renderHeader.init(p);
        requirejs.undef(module);
        var patharr = p.split('/');
        require([module], function(module) {
            if(module){
                ko.cleanNode($('#content')[0]);
                var _params = {el:"#content"};
                if(params){
                    _params = $.extend(_params,{params:params});
                }
                new module(_params);
            }
        })
    }
    //首页
    window.addRouter("index/index");

    //初始化路由
    router.init();
    //用于类参照
    window.routeTo = function (p, params, target) {
      var module  = p;
      var content = "#content";
      if (target)
        content = target;
      requirejs.undef(module);
      require([module], function (module) {
        ko.cleanNode($(content)[0]);
        $(content).html('');
        $(content).html(module.template);
        ko.applyBindings(module.model, $(content)[0]);
        module.init(params);
      })
    }

    var currentpage = window.location.hash;
    if(currentpage){
        currentpage=currentpage.split('?')[0];
        if(currentpage.indexOf("#")==0){
            currentpage=currentpage.split('#')[1];
        }
        // 不这么加会出现 如果路由已经初始化之后,还会再来一次页面初始化,造成多次请求
        if(!(curRoute&& curRoute == currentpage)) {
            //TODO:带参数的路由怎么重新出发
            if(currentpage.indexOf("goods")>=0 || currentpage.indexOf("accountcenter")>=0|| currentpage.indexOf("permit_header")>=0){
                window.router.setRoute(currentpage);
            }
            else{
                initPage("static/pages/"+currentpage);
            }
        }
    }
    else{
        window.router.setRoute("index/index");
//      initPage("static/pages/index/index");
    } 
    // 工作台的页面不加头尾
    if(currentpage.indexOf("permit") >= 0){
      return
    }else{
      initCommonView();
    }
    function initCommonView(){
        //底部
        new Footer({el:"#footer"});
        //头部
        //header是动态的,此处追加函数作补丁
        window.renderHeader = new Header({el:"#header"});
        window.renderHeader.init=function(path){
          var patharr = path.split('/');
          if (patharr[patharr.length-1] != 'index') {
             $(window.renderHeader.el).find('#modelbox').hide()
          } else {
            if(ipu_token) {
              $(window.renderHeader.el).find('#modelbox').show()
            }
          }
        }
    }
});
